package com.graduate.bssp.controller;

import com.graduate.base.entity.PageInfo;
import com.graduate.bssp.entity.Tvshow;
import com.graduate.bssp.entity.Tvshow;
import com.graduate.bssp.service.ITvshowService;
import com.graduate.common.constants.MessageConstants;
import com.graduate.common.dto.AjaxResult;
import com.graduate.common.util.DateUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.graduate.base.controller.BaseController;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 电视剧 前端控制器
 * </p>
 *
 * @author yongqiangyao
 * @since 2018-02-05
 */
@Controller
@RequestMapping("/bssp/tvShow")
public class TvshowController extends BaseController {
    @Autowired
    private ITvshowService tvshowService;

    @Value("${imageFilePath}")
    private String imageFilePath;

    @RequestMapping("/list")
    @ResponseBody
    public AjaxResult pageData(PageInfo<Tvshow> pageInfo2){
        AjaxResult ajaxResult=null;
        try{
            pageInfo2.count();
            List<Tvshow> tvshowList=tvshowService.selectTvShowList(pageInfo2);
            ajaxResult=json(MessageConstants.BSSP_STATUS_SUCCESS,tvshowList);
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult=result(MessageConstants.BSSP_STATUS_FAIL);
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/save")
    public Map<String,Object> save(Tvshow tvShow, @RequestParam("imageFile")MultipartFile file, HttpServletRequest request)throws Exception{
        if(!file.isEmpty()){
            // 获取文件名
            String fileName = file.getOriginalFilename();
            // 获取文件的后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            String newFileName= DateUtil.getCurrentDateStr()+suffixName;
            StringBuffer sb=new StringBuffer();
            sb.append("tvShow"+newFileName);
            FileUtils.copyInputStreamToFile(file.getInputStream(), new File(imageFilePath+newFileName));
            tvShow.setImgSrc(sb.toString());
        }
        //film.setPublishDate(new Date());
        Map<String, Object> resultMap = new HashMap<>();
        tvshowService.inserttvShow(tvShow);
        //initSystem.loadData(request.getServletContext());
        resultMap.put("success", true);
        return resultMap;
    }
    /**
     * 分页查询电影
     * @param page
     * @param rows
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/adminList")
    public Map<String,Object> list(Tvshow tvshow, @RequestParam(value="page",required=false)Integer page, @RequestParam(value="rows",required=false)Integer rows)throws Exception{
        PageInfo<Tvshow> pageInfo=new PageInfo<>();
        pageInfo.setpageNumber(rows);
        pageInfo.setCurrentPage(page);
        pageInfo.setAnyName(tvshow.getName());
        pageInfo.count();
        List<Tvshow> filmList=tvshowService.selectTvShowList(pageInfo);
        int total=tvshowService.selectTvshowCountList(pageInfo);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("rows", filmList);
        resultMap.put("total", total);
        return resultMap;
    }
    /**
     * id查询
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/findById")
    public Tvshow findById(@RequestParam(value="id")Integer id)throws Exception{
        Tvshow film=tvshowService.findById(id);
        return film;
    }
    /**
     * id查询
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/id")
    public Tvshow findByIdMenu(@RequestParam(value="id")Integer id)throws Exception{
        Tvshow film=tvshowService.findById(id);
        return film;
    }
    @ResponseBody
    @RequestMapping("/updateById")
    public Map<String,Object> updateById(Tvshow film, @RequestParam("imageFile")MultipartFile file, HttpServletRequest request)throws Exception{
        if(!file.isEmpty()){
            // 获取文件名
            String fileName = file.getOriginalFilename();
            // 获取文件的后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            String newFileName= DateUtil.getCurrentDateStr()+suffixName;
            StringBuffer sb=new StringBuffer();
            sb.append("tvshow"+newFileName);
            FileUtils.copyInputStreamToFile(file.getInputStream(), new File(imageFilePath+newFileName));
            film.setImgSrc(sb.toString());
        }
        Map<String, Object> resultMap = new HashMap<>();
        if (tvshowService.updateFilm(film)>0) {
            resultMap.put("success", true);
            return resultMap;
        }
        resultMap.put("defeat", false);
        return resultMap;
    }
    /**
     * 删除电影信息
     * @param ids
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Map<String,Object> delete(@RequestParam(value="ids")String ids,HttpServletRequest request)throws Exception{
        String []idsStr=ids.split(",");
        Map<String, Object> resultMap = new HashMap<>();
        for(int i=0;i<idsStr.length;i++){
            Integer filmId=Integer.parseInt(idsStr[i]);
            if (tvshowService.deleteTvshowById(filmId)>0){
                resultMap.put("success", true);
            }
            else {
                resultMap.put("errorInfo", "删除失败！");
            }
        }
        return resultMap;
    }
}
